#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;

class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int Max = *max_element(nums.begin(), nums.end());
        vector<int> v(Max + 1);
        for (int i = 0; i < nums.size(); ++i)
        {
            v[nums[i]] += nums[i];
        }

        vector<int> f(v.size());
        vector<int> g(v.size());
        f[0] = v[0];
        for (int i = 1; i < v.size(); ++i)
        {
            f[i] = g[i - 1] + v[i];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[v.size() - 1], g[v.size() - 1]);
    }
};